from __future__ import annotations

from dataclasses import dataclass
from typing import List, Dict

from gssgca.transmission_line.state_equation import stress_temperature_state, wind_ice_combination


@dataclass
class SagWeatherPoint:
    weather: str
    tension_kN: float
    sag_m: float


def sag_under_multiple_weathers(
    span_m: float,
    unit_weight_kN_m: float,
    elastic_modulus_MPa: float,
    area_mm2: float,
    alpha_per_C: float,
    temp_ref_C: float,
    initial_tension_kN: float,
    conductor_diameter_mm: float,
    weather_cases: List[Dict],
) -> List[SagWeatherPoint]:
    """代表档距下的多气象应力-弧垂计算。

    weather_cases: 数组，每项可含 {name, wind_kPa, temp_C, ice_mm}
    会将覆冰转为线重增量叠加再调用应力-温度方程。
    """

    results: List[SagWeatherPoint] = []
    for case in weather_cases:
        name = str(case.get("name", "case"))
        temp_C = float(case.get("temp_C", temp_ref_C))
        ice_mm = float(case.get("ice_mm", 0.0))
        combo = wind_ice_combination(
            base_wind_pressure_kPa=float(case.get("wind_kPa", 0.0)),
            reference_temp_C=temp_ref_C,
            design_temp_C=temp_C,
            ice_mm=ice_mm,
            conductor_diameter_mm=conductor_diameter_mm,
        )
        w_eff = unit_weight_kN_m + combo.weight_increase_kN_m
        state = stress_temperature_state(
            span_m=span_m,
            unit_weight_kN_m=w_eff,
            elastic_modulus_MPa=elastic_modulus_MPa,
            area_mm2=area_mm2,
            alpha_per_C=alpha_per_C,
            temp_ref_C=temp_ref_C,
            temp_work_C=temp_C,
            initial_tension_kN=initial_tension_kN,
        )
        results.append(SagWeatherPoint(weather=name, tension_kN=state.horizontal_tension_kN, sag_m=state.sag_m))
    return results


